/*
   Copyright 2022 cmanlh

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/**
 * text input
 * 
 */
import JscConfig from './config.js';
import JscDatabind from './databind.js';
export default class JscInputText extends JscDatabind {
    static get observedAttributes() { return ['required', 'readonly', 'disabled', 'placeholder', 'value', 'jscbind', 'maxlength'] }

    #value;
    #input = undefined;
    #required = false;
    #jscBind = null;
    #maxlength = null;

    constructor() {
        super();
        this.#init();
        this.#render();
    }

    connectedCallback() {
        this.#bindEventForInput();
    }

    disconnectedCallback() {
    }

    attributeChangedCallback(name, oldValue, newValue) {
        switch (name) {
            case 'value': {
                if (oldValue != newValue) {
                    this.value = newValue;
                }
                break;
            }
            case 'required': {
                if (oldValue != newValue) {
                    if (null == newValue) {
                        this.#required = false;
                    } else {
                        this.#required == newValue;
                    }
                }
                break;
            }
            case 'disabled': {
                if (oldValue != newValue) {
                    if (newValue || '' == newValue) {
                        this.#input.setAttribute('disabled', true);
                    } else {
                        this.#input.removeAttribute('disabled');
                    }
                }
                break;
            }
            case 'jscbind': {
                if (oldValue != newValue) {
                    this.#jscBind = newValue;
                }
                break;
            }
        }
    }

    bindRefresh(bindStamp) {
        if (super.bindRefresh(bindStamp)) {
            if (null != this.bind && null != this.#jscBind) {
                this.#value = this.fetchBindValue(this.#jscBind);
                this.#input.value = this.#value;
            }
        }
    }

    set value(val) {
        let self = this;
        let oldValue = self.#value;
        if (oldValue == val) {
            return;
        }
        self.#value = val;
        this.#input.value = self.#value;
        this.updateBindValue(this.#jscBind, this.#value);
        this.checkValidity();
    }

    get value() {
        return this.#value;
    }

    #init() {
        let required = this.getAttribute('required');
        if (null == required) {
            this.#required = false;
        } else {
            this.#required = true;
        }

        this.#jscBind = this.getAttribute('jscbind');
    }

    #render() {
        const shadowRoot = this.attachShadow({ mode: 'open' });
        shadowRoot.innerHTML = `
        <link rel='stylesheet' type='text/css' href='${JscConfig.cssUrl}'/>
        <input type='text' class='inputText'>
        `;
        this.#input = this.shadowRoot.querySelector('input');

        if (this.getAttribute('placeholder')) {
            this.#input.setAttribute('placeholder', this.getAttribute('placeholder'));
        }

        if (this.getAttribute('readonly')) {
            this.#input.setAttribute('readonly', true);
        }

        if (this.getAttribute('disabled')) {
            this.#input.setAttribute('disabled', true);
        }

        this.#maxlength = this.getAttribute('maxlength');
        if (this.#maxlength) {
            this.#input.setAttribute('maxlength', this.#maxlength);
        }
    }

    #bindEventForInput() {
        let self = this;
        self.#input.addEventListener('focusout', function (e) {
            self.value = self.#input.value;
        });
    }

    checkValidity() {
        let errMsg = '';
        if (this.#required && (undefined == this.#value || null == this.#value || '' == this.#value)) {
            errMsg = '该字段不能为空';
        } else if (null != this.#maxlength && this.#value.length > this.#maxlength) {
            errMsg = '最大允许输入'.concat(this.#maxlength).concat('个字符');
        }

        this.#input.setCustomValidity(errMsg);
        this.#input.reportValidity();

        if ('' === errMsg) {
            return true;
        } else {
            return false;
        }
    }
}

if (!customElements.get('jsc-input-text')) {
    window.customElements.define("jsc-input-text", JscInputText);
}